<script setup name="SelectLawFirmDialog" lang="ts">
import { listLawFirm, getLawFirm } from '@/api/lawfirm/lawfirm';
import { LawFirmVo, LawFirmQuery, LawFirmForm } from '@/api/lawfirm/lawfirm/types';
import {assignOrder, getSpuOrder} from '@/api/trade/order';
import { SpuOrderAssignVo, SpuOrderVo} from '@/api/trade/order/types';
import { RouteLocationNormalized } from 'vue-router';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const lawFirmList = ref<LawFirmVo[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const defaultSpuOrderVo = ref<SpuOrderVo>();
const defaultOrderId = ref('');
const queryFormRef = ref<ElFormInstance>();
const lawFirmFormRef = ref<ElFormInstance>();
const defaultAssignOrder = ref<defaultSpuOrderVo>();
const route = useRoute();
const initFormData: LawFirmForm = {
  lawFirmId: undefined,
  lawFirmName: undefined,
  lawFirmLogo: undefined,
  lawFirmType: undefined,
  status: '0',
  lawFirmDesc: undefined,
  lawFirmAddress: undefined,
  companyName: undefined,
  license: undefined,
  creditCode: undefined,
  companyAddress: undefined,
  companyAuth: undefined,
  province: undefined,
  city: undefined,
  district: undefined,
  rating: undefined,
  adminUserId: undefined,
  curated: undefined,
  followers: undefined,
  special: undefined,
  certification: undefined,
  yearServiceTime: undefined,
  contactPhone: undefined
};
const data = reactive<PageData<LawFirmForm, LawFirmQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lawFirmName: undefined,
    special: undefined,
    lawFirmType: undefined,
    contactPhone: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams, form, rules} = toRefs(data);
const state = reactive({
  dialog: {
    visible: false,
    title: ''
  }
});

/** 查询律所信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listLawFirm(queryParams.value);
  lawFirmList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

const { dialog } = toRefs(state);
const openDialog = async (row: SpuOrderAssignVo) => {
  defaultAssignOrder.value = row;
  await getList();
  dialog.value.visible = true;
  dialog.value.title = '指派处理人';
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: LawFirmVo[]) => {
  ids.value = selection.map(item => item.lawFirmId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

const handleAssign = async (row?: LawFirmVo) => {
  const _lawFirmId = row?.lawFirmId || ids.value[0];
  await proxy?.$modal.confirm(`是否确认指派${row.lawFirmName}处理该订单？`).finally(() => loading.value = false);
  await assignOrder(defaultAssignOrder.value.orderId, _lawFirmId);
  proxy?.$modal.msgSuccess('操作成功');
  emit('refresh');
  dialog.value.visible = false;
};

const cancel = () => {
  dialog.value.visible = false;
};

defineExpose({
  openDialog
});
</script>

<template>
  <el-dialog :title="dialog.title" v-model="dialog.visible" width="1080px" append-to-body>
    <div class="p-2">
      <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                  :leave-active-class="proxy?.animate.searchAnimate.leave">
        <div class="mb-[10px]">
          <el-card shadow="hover">
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                  <el-form-item label="名称" prop="lawFirmName">
                    <el-input v-model="queryParams.lawFirmName" placeholder="请输入名称" clearable
                              @keyup.enter="handleQuery"/>
                  </el-form-item>
                  <el-form-item label="擅长" prop="special">
                    <el-input v-model="queryParams.special" placeholder="请输入擅长" clearable @keyup.enter="handleQuery"/>
                  </el-form-item>
                  <el-form-item label="联系电话" prop="contactPhone">
                    <el-input v-model="queryParams.contactPhone" placeholder="请输入联系电话" clearable
                              @keyup.enter="handleQuery"/>
                  </el-form-item>
                  <el-form-item label="类型" prop="lawFirmType">
                    <el-select v-model="queryParams.lawFirmType" placeholder="请选择类型" clearable>
                      <el-option value="1" label="律师"></el-option>
                      <el-option value="2" label="律所"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                  </el-form-item>
            </el-form>
          </el-card>
        </div>
      </transition>
      <el-card shadow="never">
        <el-table v-loading="loading" :data="lawFirmList" @selection-change="handleSelectionChange">
          <!--        <el-table-column type="selection" width="55" align="center" />-->
          <el-table-column label="律所ID" align="center" prop="lawFirmId" v-if="false"/>
          <el-table-column label="类型" align="center" prop="lawFirmType" width="100">
            <template #default="scope">
              <el-tag v-if="scope.row.lawFirmType === '1'">律师</el-tag>
              <el-tag type="warning" v-else>律所</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="头像" align="center" prop="lawFirmLogo" width="140">
            <template #default="scope">
              <image-preview :src="scope.row.lawFirmLogo" :width="59" :height="59"/>
            </template>
          </el-table-column>
          <el-table-column label="名称" align="center" prop="lawFirmName" width="200"/>
          <el-table-column label="联系电话" align="center" prop="contactPhone" width="160"/>
          <el-table-column label="擅长" align="center" prop="special"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="指派该律师" placement="top">
                <el-button link type="primary" icon="CircleCheck" @click="handleAssign(scope.row)" ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList"/>
      </el-card>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="cancel">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
